package de.openlaboratory.labhub.core.server.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import de.openlaboratory.labhub.core.ApplicationResources;
import de.openlaboratory.labhub.core.framework.Command;
import de.openlaboratory.labhub.core.framework.RuntimeFactory;
import de.openlaboratory.labhub.core.server.RequestHelper;

public class DefaultFrontController extends LabHubServlet
{
	private static final long serialVersionUID = 585421997597044325L;

	// Initializes the servlet.
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
	}

	// Destroys the servlet.
	public void destroy()
	{
	}

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
	 * methods.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 */
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			java.io.IOException
	{
		ApplicationResources resource = ApplicationResources.getInstance();
		RequestHelper helper = new RequestHelper(request);
		Command cmdHelper = helper.getCommand();

		String counter = getParameterString("");
		RuntimeFactory.getLogger().info(counter);
		/**
		 * EXECUTE COMMAND
		 */
		try
		{
			// Command helper perform custom operation
			cmdHelper.execute(request);
		}
		catch (Exception e)
		{
			request.setAttribute(resource.getMessageAttr(), "Exception occurred : " + e.getMessage());
			resource.getErrorPage(e);
		}

		/**
		 * DISPATCH REQUEST
		 */
		// dispatch control to view
		dispatch(request, response, cmdHelper);
	}

	/**
	 * Handles the HTTP <code>GET</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			java.io.IOException
	{
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			java.io.IOException
	{
		processRequest(request, response);
	}

	/** Returns a short description of the servlet */
	public String getServletInfo()
	{
		return "Open Laboratory LabHub Front Controller Servlet";
	}

	protected void dispatch(HttpServletRequest request, HttpServletResponse response, Command command)
			throws javax.servlet.ServletException, java.io.IOException
	{
		RequestDispatcher dispatcher = new DefaultRequestDispatcher(); // getServletContext().getRequestDispatcher(page);
		dispatcher.forward(request, response);
	}
}